import { ApiResponse, User } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

// 获取用户信息
export function getUser(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}
// 获取个人资料
export function getUserProfile(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get('/user/profile')
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data
    })
  }
}
// 提交个人资料
export function updateUserProfile(key: string, value: string): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', {
      [key]: value
    })
    // 重新渲染
    dispatch(getUserProfile())
  }
}

// 上传图片
export function updateUserPhoto(fd: FormData): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/photo', fd)
    // 重新渲染
    dispatch(getUserProfile())
  }
}